package com.dlc.beijingrfid.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.dlc.commonlibrary.utils.LogPlus;
import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.commonlibrary.widget.popup.CommonPopupList;
import com.dlc.beijingrfid.R;
import com.dlc.beijingrfid.base.BaseActivity;
import com.dlc.beijingrfid.base.Constants;
import com.dlc.beijingrfid.net.bean.StockNumberBean;
import com.dlc.beijingrfid.net.bean.WareFrameBean;
import com.dlc.beijingrfid.net.bean.WarehouseBean;
import com.dlc.beijingrfid.net.protocol.CountingProto;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 货物盘点
 */
public class CountingProductActivity extends BaseActivity {

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.tv_warehouse) TextView mTvWarehouse;
    @BindView(R.id.drop_warehouse) FrameLayout mDropWarehouse;
    @BindView(R.id.tv_warehouse_room) TextView mTvWarehouseRoom;
    @BindView(R.id.drop_warehouse_room) FrameLayout mDropWarehouseRoom;
    @BindView(R.id.tv_counting_number) TextView mTvCountingNumber;
    @BindView(R.id.drop_counting_number) FrameLayout mDropCountingNumber;
    @BindView(R.id.btn_count) Button mBtnCount;
    
    
    private CommonPopupList mWarehousePopup;
    private CommonPopupList mWarehouseRoomPopup;
    private CommonPopupList mCountingNumberPopup;

    private List<WarehouseBean.DataBean> warehouseList;
    private List<WareFrameBean.DataBean> wareframeList;
    private List<StockNumberBean.DataBean> stockNumberList;
    private String warehouseId;
    private String wareFrameId;
    private String stockNumberId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitleBar.leftExit(this);

        initView();
        initCountingNumberData();
        initWarehouseData();
    }

    private void initView() {

        mCountingNumberPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(mDropCountingNumber)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        // TODO: 2017/7/20
                        mTvCountingNumber.setText(item);

                        //String id = warehouseList.get(position).getId();
                        stockNumberId = item;
                    }
                })
                .build();

        mWarehousePopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(mDropWarehouse)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();
                        // TODO: 2017/7/20
                        mTvWarehouse.setText(item);

                        String id = warehouseList.get(position).getId();
                        warehouseId = id;
                        initWareframeData(id);
                    }
                })
                .build();

        mWarehouseRoomPopup = CommonPopupList.newBuilder(this)
                .setWidthEquals(mDropWarehouseRoom)
                .setOnSelectItemListener(new CommonPopupList.OnSelectItemListener() {
                    @Override
                    public void onSelectItem(CommonPopupList popup, String item, int position) {
                        popup.dismiss();

                        wareFrameId = wareframeList.get(position).getId();
                        mTvWarehouseRoom.setText("库位"+item);
                    }
                })
                .build();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_counting_product;
    }

    /**
     * 设置下拉菜单数据
     */
    private void setPopupData(int item_type,String[] data) {
        switch (item_type){
            case Constants.ITEM_TYPE.WAREHOUSE:
                mWarehousePopup.setData(data,false);
                break;
            case Constants.ITEM_TYPE.LIBRARY:
                mWarehouseRoomPopup.setData(data,true);
                break;
            case Constants.ITEM_TYPE.COUNTING_NUMBER:
                mCountingNumberPopup.setData(data,true);
                break;
        }
    }

    /**
     * 获取仓库数据
     */
    private void initWarehouseData() {
        CountingProto.get().getWarehouseList(null).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<WarehouseBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                LogPlus.e(e.getMessage());
            }

            @Override
            public void onNext(WarehouseBean warehouseBean) {
                warehouseList = warehouseBean.getData();
                if(warehouseList != null){
                    String [] warehouses = new String[warehouseList.size()];
                    for(int i = 0;i<warehouseList.size();i++){
                        warehouses[i] = warehouseList.get(i).getTitle();
                    }
                    setPopupData(Constants.ITEM_TYPE.WAREHOUSE,warehouses);
                }
            }
        });
    }

    /**
     * 获取货架数据
     */
    private void initWareframeData(String pid) {
        CountingProto.get().getWareFrameList(pid).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<WareFrameBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(WareFrameBean wareFrameBean) {
                        wareframeList = wareFrameBean.getData();
                        if(wareframeList != null){
                            String [] wareFrames = new String[wareframeList.size()];
                            for(int i = 0;i<wareframeList.size();i++){
                                wareFrames[i] = wareframeList.get(i).getFrameno();
                            }
                            setPopupData(Constants.ITEM_TYPE.LIBRARY,wareFrames);
                        }
                    }
                });
    }

    /**
     * 获取盘点订单编号
     */
    private void initCountingNumberData() {
        CountingProto.get().getStockNumber("").
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<StockNumberBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogPlus.e(e.getMessage());
                    }

                    @Override
                    public void onNext(StockNumberBean bean) {
                        stockNumberList = bean.getData();
                        if(stockNumberList != null){
                            String [] stockNumbers = new String[stockNumberList.size()];
                            for(int i = 0;i<stockNumberList.size();i++){
                                stockNumbers[i] = stockNumberList.get(i).getStockno();
                            }
                            setPopupData(Constants.ITEM_TYPE.COUNTING_NUMBER,stockNumbers);
                        }
                    }
                });
    }

    @OnClick({ R.id.drop_warehouse, R.id.drop_warehouse_room, R.id.btn_count,R.id.drop_counting_number })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.drop_warehouse:
                if(warehouseList != null){
                    mWarehousePopup.showAsDropDown(view);
                }
                break;
            case R.id.drop_warehouse_room:
                mWarehouseRoomPopup.showAsDropDown(view);
                break;
            case R.id.drop_counting_number:
                mCountingNumberPopup.showAsDropDown(view);
                break;
            case R.id.btn_count:

                if(TextUtils.isEmpty(stockNumberId)){
                    ToastUtil.show(this,"请选择盘点单号");
                    return;
                }
                if(TextUtils.isEmpty(warehouseId)){
                    ToastUtil.show(this,"请选择仓库");
                    return;
                }
//                if(TextUtils.isEmpty(wareFrameId)){
//                    ToastUtil.show(this,"请选择库位");
//                    return;
//                }
                Intent intent = new Intent(this,CountingProduct2Activity.class);
                intent.putExtra("wareHouse",mTvWarehouse.getText().toString().trim());
                intent.putExtra("wareFrame",mTvWarehouseRoom.getText().toString().trim());
                intent.putExtra("warehouseId",warehouseId);
                intent.putExtra("wareFrameId",wareFrameId);
                intent.putExtra("stockno",stockNumberId);
                startActivity(intent);
                break;
        }
    }



}
